import numpy as np

from optimizer.vcmtsp.deprecated.data_loader import Data
from optimizer.vcmtsp.deprecated.ABC import ABC
from optimizer.vcmtsp.deprecated.ACO import ACO
from deprecated.GA import GA

from utils.tools import getDate, make_dir, save_routes

if __name__ == '__main__':
    date = getDate()
    make_dir(f'./results/{date}')

    cases_1 = ['att48',
               'eil51',
               'berlin52',
               'eil76',
               'eil101',
               'ch130']

    cases_2 = ['kroA100',
               'kroA150',
               'kroA200',
               'kroB100',
               'kroB150',
               'kroB200']

    run_times = 5
    for case in cases_1:
        for m in range(2, 9):
            # case, m = 'eil51', 4

            print(f'working on {case} with {m} salesmen ...')
            data = Data(name=case, m=m)

            ga_routes_1, ga_fitness_1 = [[], 1e99], []
            ga_routes_2, ga_fitness_2 = [[], 1e99], []
            abc_routes, abc_fitness = [[], 1e99], []
            aco_routes, aco_fitness = [[], 1e99], []

            for run in range(run_times):

                # ga1 = GA(data=data, NP=100, Pc=0.3, Pm=0.5, method='1')
                # ga1.optimize(iterations=100)
                # ga_fitness_1.append(ga1.life.gBest)
                # if ga_routes_1[1] > ga1.gBest[1]:
                #     ga_routes_1 = ga1.gBest

                ga2 = GA(data=data, NP=100, Pc=0.3, Pm=0.5, method='2')
                ga2.optimize(iterations=100)
                ga_fitness_2.append(ga2.life.gBest)
                if ga_routes_2[1] > ga2.gBest[1]:
                    ga_routes_2 = ga2.gBest

                abc = ABC(data=data, NP=100, drop_prob=0.3, SL=50)
                abc.optimize(iterations=100)
                abc_fitness.append(abc.life.gBest)
                if abc_routes[1] > abc.gBest[1]:
                    abc_routes = abc.gBest

                aco = ACO(data=data, NP=10, alpha=1, beta=2.5, rho=0.1, xee=0.1)
                aco.optimize(iterations=100)
                aco_fitness.append(aco.life.gBest)
                if aco_routes[1] > aco.gBest[1]:
                    aco_routes = aco.gBest

            np.savetxt(f'./results/{date}/{case}_m{m}_fitness_GA_1.txt', ga_fitness_1, fmt='%f')
            save_routes(f'./results/{date}/{case}_m{m}_routes_GA_1.txt', ga_routes_1[0])

            np.savetxt(f'./results/{date}/{case}_m{m}_fitness_GA_2.txt', ga_fitness_2, fmt='%f')
            save_routes(f'./results/{date}/{case}_m{m}_routes_GA_2.txt', ga_routes_2[0])

            np.savetxt(f'./results/{date}/{case}_m{m}_fitness_ABC.txt', abc_fitness, fmt='%f')
            save_routes(f'./results/{date}/{case}_m{m}_routes_ABC.txt', abc_routes[0])

            np.savetxt(f'./results/{date}/{case}_m{m}_fitness_ACO.txt', aco_fitness, fmt='%f')
            save_routes(f'./results/{date}/{case}_m{m}_routes_ACO.txt', aco_routes[0])
